//
//  LeftMenuViewController.m
//  51-Tour
//
//  Created by xiang ying on 14-3-18.
//  Copyright (c) 2014年 xiang ying. All rights reserved.
//

#import "LeftMenuViewController.h"
#import "ServiceManage.h"
#import "UIImageView+WebCache.h"

@interface LeftMenuViewController ()<UITableViewDataSource,UITableViewDelegate>{
    NSArray *_itemsArray,*_imagesArray;
    
    UITableView *menu;
    UILabel *version;
}

@end

@implementation LeftMenuViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(languageReset:) name:@"RESETLANGUAGE" object:nil];

    }
    return self;
}

- (void)languageReset:(NSNotification *)noti{
    _itemsArray = @[[[DataCenter instance] getStringForKey:@"LeftDownload"],
                    [[DataCenter instance] getStringForKey:@"Share"],
                    [[DataCenter instance] getStringForKey:@"About"],
                    [[DataCenter instance] getStringForKey:@"More"]];
    
    version.text = [[[DataCenter instance] getStringForKey:@"Version"] stringByAppendingString:[[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"]];
    [menu reloadData];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    _itemsArray = @[[[DataCenter instance] getStringForKey:@"LeftDownload"],
                    [[DataCenter instance] getStringForKey:@"Share"],
                    [[DataCenter instance] getStringForKey:@"About"],
                    [[DataCenter instance] getStringForKey:@"More"]];
    
    _imagesArray = @[@"ic-download_05",@"ic-share_06",@"ic-info_10",@"ic-more_10"];
    
    if (IOS7) {
        menu = [[UITableView alloc] initWithFrame:CGRectMake(0, self.contentView.frame.origin.y-44, 320, self.contentView.frame.size.height+44)];
    }else{
        menu = [[UITableView alloc] initWithFrame:self.contentView.frame];
    }
    menu.separatorStyle = UITableViewCellSeparatorStyleNone;
    [menu setBackgroundColor:[UIColor clearColor]];
    menu.dataSource = self;
    menu.delegate = self;
    [self.view addSubview:menu];
    
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 200, 260, 40)];
    imageView.userInteractionEnabled = YES;
    [self.view addSubview:imageView];
    
    version = [[UILabel alloc] initWithFrame:CGRectMake(0, menu.frame.size.height-40, 260, 40)];
    version.backgroundColor = [UIColor clearColor];
    version.textAlignment = NSTextAlignmentCenter;
    version.font = [UIFont systemFontOfSize:15];
    version.textColor = [UIColor lightGrayColor];
    version.text = [[[DataCenter instance] getStringForKey:@"Version"] stringByAppendingString:[[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"]];
    [self.view addSubview:version];
    
    [ServiceManage adRequestInfo:nil completion:^(RespondDTO *respond) {
        if (respond.flag == respond_success) {
            NSDictionary *data = respond.data;
            
            __block UIImageView *imgView = imageView;
            [imageView setImageWithURL:[NSURL URLWithString:data[@"imageUrl"]] placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
                if (image) {
                    CGFloat height = 260*image.size.height/image.size.width;
                    if (height>200) {
                        height = 200;
                    }
                    [imgView setFrame:CGRectMake(0, menu.frame.size.height-40-height, 260, height)];
                    
                    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(loadWebAd:)];
                    [imgView addGestureRecognizer:tap];
                }
            }];
        }
    }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)loadWebAd:(UIGestureRecognizer*)gesture{
    [[UIManagement shareInstance] showAD];
}

#pragma mark - UITableViewDataSource & UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _itemsArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.backgroundColor = [UIColor clearColor];
        cell.textLabel.font = [UIFont systemFontOfSize:16];

        UIImageView *_line = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"list-line_14"]];
        _line.frame = CGRectMake(0, 43, 320, 1);
        [cell addSubview:_line];
    }
    cell.textLabel.text = _itemsArray[indexPath.row];
    cell.imageView.image = [UIImage imageNamed:_imagesArray[indexPath.row]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    switch (indexPath.row) {
        case 0:{
        //离线下载
            [[UIManagement shareInstance] showDownload];
        }
            
            break;
        
        case 1:{
        //分享好友
            [[UIManagement shareInstance] showShare];
        }
            break;
        
        case 2:{
        //关于
            [[UIManagement shareInstance] showAbout];
        }
            break;
       
        case 3:{
        //更多
            [[UIManagement shareInstance] showMore];
        }
            
        default:
            break;
    }
}
@end
